package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/gomodule/redigo/redis"
	uuid "github.com/satori/go.uuid"
	"strings"
)

var host = Host

type MyJson struct {
	Status string // s\l
	Msg    interface{}
}
type API struct {
	beego.Controller
}

// 点击投票 在console查看票数
func (c *API) Toupiao() {
	var result MyJson
	var out string
	//fmt.Println("是否为ajax请求",c.IsAjax())
	id := c.GetString("id")

	conn, err := redis.Dial("tcp", host)
	reportError(err)
	defer func() { _ = conn.Close() }()

	_, err = redis.String(conn.Do("ZINCRBY", "toupiao", 1, id))
	reportError(err)

	dabs, err := redis.Values(conn.Do("zrevrange", "toupiao", 0, -1, "withscores"))

	for k, value := range dabs {
		s, _ := redis.String(value, err)
		if k%2 != 0 {
			out += ":" + s
		} else {
			out += "--" + s
		}

	}
	result.Msg = out
	c.Data["json"] = &result
	c.ServeJSON() //callback

}

func (c *API) BaoMing() {
	var result MyJson
	name := c.GetString("name")

	conn, err := redis.Dial("tcp", host)
	defer func() { _ = conn.Close() }()
	reportError(err)
	nums, err := conn.Do("SADD", "baoming", name)
	var zero int64 = 0
	if nums == zero {
		// data["Status"] == "l" 用于前端js判断
		result.Status = "l"
	} else {
		result = MyJson{
			Status: "s",
			Msg:    nums,
		}
	}
	c.Data["json"] = &result
	c.ServeJSON()
}

// 抽奖
func (c *API) Choujiang() {
	result := MyJson{}

	conn, err := redis.Dial("tcp", host)
	defer func() { _ = conn.Close() }()
	reportError(err)

	winners, err := redis.Values(conn.Do("SRandMember", "baoming", 1))
	reportError(err)
	winner, err := redis.String(winners[0], err)
	result.Msg = winner
	c.Data["json"] = &result
	c.ServeJSON()
}

func (c *API) GetYZM() {
	result := MyJson{}
	conn, err := redis.Dial("tcp", host)
	defer func() { _ = conn.Close() }()
	reportError(err)
	uid := uuid.NewV4()

	_, err = conn.Do("setex", "xx", 2, uid)
	reportError(err)
	result.Msg = uid.String()
	c.Data["json"] = &result
	c.ServeJSON()
}

func (c *API) CheckYZM() {
	result := MyJson{}
	given := c.GetString("uid")
	conn, err := redis.Dial("tcp", host)
	defer func() { _ = conn.Close() }()
	reportError(err)

	uid, err := redis.String(conn.Do("get", "xx"))
	reportError(err)
	if uid == given {
		result.Status = "s"
	} else {
		result.Status = "l"
	}
	c.Data["json"] = &result
	c.ServeJSON()
}

func (c *API) FindInter() {
	var (
		out    string
		result = MyJson{}
	)
	u, f := strings.Split(c.GetString("u"), ","), strings.Split(c.GetString("f"), ",")
	conn, err := redis.Dial("tcp", host)
	defer func() { _ = conn.Close() }()
	reportError(err)
	_, err = conn.Do("sadd", transEmptyInterface("tmp1", u)...)
	_, err = conn.Do("sadd", transEmptyInterface("tmp2", f)...)
	reportError(err)
	defer func() {
		_, err = conn.Do("del", "tmp1")
		_, err = conn.Do("del", "tmp2")
	}()

	dabs, err := redis.Values(conn.Do("SINTER", "tmp1", "tmp2"))

	for _, value := range dabs {
		s, _ := redis.String(value, err)
		out += s
	}
	result.Msg = out
	c.Data["json"] = &result
	c.ServeJSON()
}

// nil != nil {} 简单答应error
func reportError(err error) {
	if err != nil {
		fmt.Println(err)
	}

}

func transEmptyInterface(k string, sg []string) []interface{} {
	out := []interface{}{k}
	for _, value := range sg {
		out = append(out, value)
	}
	return out
}
